﻿@{
    ViewBag.Title = "商品管理";
    ViewBag.PathToBtn = "/CommodityLib/List";
    ViewBag.TextBtn = "查看库商品";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="row">
    <div class="col-lg-12" />
    <div class="panel panel-default">
        <div class="panel-body">
            <a id="btnPutaway" onclick="OnAShelf(1)" href="javascript:;" class="btn btn-success">批量上架</a>
            <a id="btnPutaway" onclick="OnAShelf(0)" href=" javascript:;" class="btn btn-primary">批量下架</a>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                商品列表
            </div>
            <div class="panel-body">
                <div class="dataTable_wrapper">
                    <table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">
                        <thead>
                            <tr>
                                <th>
                                    <input type="checkbox" name="select_all" value="1" id="select-all">
                                </th>
                                <th>商品名称</th>
                                <th>店铺名称</th>
                                <th>卖价</th>
                                <th>库存</th>
                                <th>显示排序</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
@section javascript{
    <script src="/Content/admin/dist/js/pagingHelper.js"> </script>
    <script type="text/javascript">
        var dataParams = {};
        var serverProcessPath = "/ShopCommodity/Listing";
        var showColumns = [
            { data: "Id" },
            { data: "CommodityName" },
            { data: "ShopName" },
            { data: "SoldPrice" },
            { data: "Quantity" },
            { data: "DisplaySort" },
            { data: "Action" }
        ];

        var redefineColumns = [
            {
                targets: 0,
                searchable: false,
                orderable: false,
                render: function(data, type, full, meta) {
                    return '<input type="checkbox" name="id[]" value="' + $('<div/>').text(data).html() + '">';
                }
            }
        ];
        pagingHelper.Paging(serverProcessPath, dataParams, showColumns, redefineColumns);

        function updateData(id) {
            document.location.href = '/ShopCommodity/Update/' + id;
        }

        function deleteData(id) {
            alertify
                .okBtn("确定")
                .cancelBtn("取消")
                .confirm("确定要删除该数据吗？", function() {
                    $.ajax({
                        type: 'post',
                        async: false,
                        url: '/ShopCommodity/Delete',
                        data: { doIds: id },
                        success: function(result) {
                            if (result) {
                                alertify.success("数据删除成功");
                                setTimeout("pagingHelper.reload();", 500);
                            } else {
                                alertify.error("数据删除失败");
                            }
                        }
                    });
                }, function() {
                });
        }

        function setSellData(id, isOnSell) {
            var alertContent = isOnSell ? "下架" : "上架";
            alertify.okBtn("确定").cancelBtn("取消").confirm("确定" + alertContent + "？", function() {
                $.ajax({
                    type: 'post',
                    url: '/ShopCommodity/SetSellStatus',
                    data: { id: id },
                    success: function(result) {
                        if (result) {
                            alertify.success("商品" + alertContent + "成功");
                            setTimeout("pagingHelper.reload();", 500);
                        } else {
                            alertify.error("商品" + alertContent + "失败");
                        }
                    }
                });
            });
        }

        var table = pagingHelper.tableInstance();

        $('#select-all').on('click', function() {
            var rows = table.rows({ 'search': 'applied' }).nodes();
            $('input[type="checkbox"]', rows).prop('checked', this.checked);
        });

        $('#dataTables-example tbody').on('change', 'input[type="checkbox"]', function() {
            if (!this.checked) {
                var el = $('#select-all').get(0);
                if (el && el.checked && ('indeterminate' in el)) {
                    el.indeterminate = true;
                }
            }
        });

        function OnAShelf(type) {
            var alertContent = type == 0 ? "下架" : "上架";
            var selectedItemIds = '';
            table.$('input[type="checkbox"]').each(function () {
                if ($.contains(document, this)) {
                    if (this.checked) {
                        selectedItemIds += this.value + ",";
                    }
                }
            });
            if (selectedItemIds == "") {
                alertify.alert("请至少选择一种商品");
                return;
            }
            alertify.okBtn("确定").cancelBtn("取消").confirm("确定批量" + alertContent + "选中的商品吗？", function () {
                $.ajax({
                    type: 'post',
                    url: "/ShopCommodity/SetSellStatusIds",
                    data: { itemIds: selectedItemIds, type: type },
                    success: function (result) {
                        if (result == true) {
                            alertify.success("操作成功!");
                            pagingHelper.reload();
                        } else {
                            alertify.error("操作失败");
                        }
                    }
                });
            });
        }
    </script>
}

